// Fill out your copyright notice in the Description page of Project Settings.


#include "Layers/MotionSubtitlesLayerBase.h"
#include "Components/SceneCaptureComponent2D.h"
#include "Engine/TextureRenderTarget2D.h"

// Sets default values for this component's properties
UMotionSubtitlesLayerBase::UMotionSubtitlesLayerBase()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
	SceneCaptureComponent2D = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCaptureComponent"));
	SceneCaptureComponent2D->SetupAttachment(this);
	SceneCaptureComponent2D->ProjectionType = ECameraProjectionMode::Orthographic;
	SceneCaptureComponent2D->SetRelativeLocation(FVector(-1000, 0, 0));
	SceneCaptureComponent2D->bCaptureEveryFrame = true;
	SceneCaptureComponent2D->bAlwaysPersistRenderingState = false;
	SceneCaptureComponent2D->ShowFlags.Atmosphere = false;
	SceneCaptureComponent2D->PrimitiveRenderMode = ESceneCapturePrimitiveRenderMode::PRM_UseShowOnlyList;
}


void UMotionSubtitlesLayerBase::RenderOutputRT() const
{
	if (SceneCaptureComponent2D != nullptr)
	{
		//SceneCaptureComponent2D->CaptureScene();
	}
}

UTextureRenderTarget2D* UMotionSubtitlesLayerBase::GetOutput() const
{
	return TextureRenderTarget2D;
}

void UMotionSubtitlesLayerBase::Init(const FUint32Vector2& ViewSize)
{
	TextureRenderTarget2D = NewObject<UTextureRenderTarget2D>(this);
	TextureRenderTarget2D->RenderTargetFormat = RTF_RGBA16f;
	TextureRenderTarget2D->ClearColor = FLinearColor::Black;
	TextureRenderTarget2D->bAutoGenerateMips = false;
	TextureRenderTarget2D->bCanCreateUAV = false;
	TextureRenderTarget2D->InitAutoFormat(ViewSize.X, ViewSize.Y);
	TextureRenderTarget2D->UpdateResourceImmediate(true);

	if (SceneCaptureComponent2D != nullptr && TextureRenderTarget2D != nullptr)
	{
		SceneCaptureComponent2D->TextureTarget = TextureRenderTarget2D;
		SceneCaptureComponent2D->OrthoWidth = TextureRenderTarget2D->SizeX;
	}
}

void UMotionSubtitlesLayerBase::Show(const FSubtitleParams& Params)
{
}

void UMotionSubtitlesLayerBase::ShowDeferred(const FSubtitleParams& Params)
{
}

void UMotionSubtitlesLayerBase::Hide()
{
}

void UMotionSubtitlesLayerBase::HideDeferred()
{
}

void UMotionSubtitlesLayerBase::Exit()
{
}


void UMotionSubtitlesLayerBase::DoScale(const EAnimTarget AnimTarget, const int32 ElementId, const float Value)
{
}

void UMotionSubtitlesLayerBase::DoTranslate(const EAnimTarget AnimTarget, const int32 ElementId, const FVector Value)
{

}

void UMotionSubtitlesLayerBase::DoFade(const EAnimTarget AnimTarget, const int32 ElementId,const float Value)
{
}

void UMotionSubtitlesLayerBase::DoDissolve(const EAnimTarget AnimTarget, const int32 ElementId,const float Value)
{
}

void UMotionSubtitlesLayerBase::DoMarquee(const EAnimTarget AnimTarget,const EAnimDirection Direction, const int32 ElementId, const float Value)
{
}

void UMotionSubtitlesLayerBase::CaptureStatus()
{
	OriginScale = GetRelativeScale3D();
}

int32 UMotionSubtitlesLayerBase::ElementCount()
{
	return 0;
}
